package com.ruanda.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruanda.domain.SysLog;
import com.ruanda.model.Result;
import com.ruanda.service.SysLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统操作日志管理控制层
 */
@Tag(name = "系统操作日志接口管理")
@RequestMapping("sys/log")
@RestController
public class SysLogcontroller {
    @Autowired
    private SysLogService sysLogService;

    /**
     * 分页查询系统操作日志
     * @param current 当前页
     * @param size 每页显示条数
     * @param userId 用户id
     * @param operation 操作描述
     * @return
     */
    @Operation(summary = "多条件分页查询系统操作日志")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation) {
        // 多条件分页查询系统操作日志
        Page<SysLog> page = new Page<>(current, size);
        page = sysLogService.page(page, new LambdaQueryWrapper<SysLog>()
                .eq(ObjectUtil.isNotEmpty(userId), SysLog::getUserId, userId)
                .like(StringUtils.hasText(operation), SysLog::getOperation, operation)
                .orderByDesc(SysLog::getCreateDate)
        );
        return Result.success(page);
    }
}